package com.splant.android.smart.UILevel.Community;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.widget.Toast;

import com.google.gson.Gson;
import com.loopj.android.http.RequestParams;
import com.splant.android.R;
import com.splant.android.smart.CommunicationLevel.sPlantAsyncHttpClient;
import com.splant.android.smart.CommunicationLevel.sPlantJsonHttpResponseHandler;
import com.splant.android.smart.Global;
import com.splant.android.smart.ModelLevel.User;
import com.splant.android.smart.SystemDefinedLevel.Utils.System_Util;

import org.apache.http.Header;
import org.json.JSONObject;

/**
 * Created by Administrator on 2017/3/8.
 */

public class Garden_Welcome_Activity extends Garden_Base_Activity {


    private static final int TIME =200;
    private static final int GO_HOME = 1000;
    private static final int GO_LOGION = 1001;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case GO_HOME:
                    login();
                    break;
                case GO_LOGION:
                    goLogin();
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.garden_welcome_activity);

        init();
    }

    private SharedPreferences preferences;

    //启动条件判断
    private void startConditionJudgment() {
        if (System_Util.ExistSDCard()) {

            if (System_Util.getSDFreeSize() < 30) {
                Toast.makeText(this, getString(R.string.system_sdcard_freesize_warning), Toast.LENGTH_LONG).show();
                finish();
            }

        } else {
            Toast.makeText(this, getString(R.string.system_nosdcard), Toast.LENGTH_LONG).show();
            finish();
        }
    }

    private void init() {

        startConditionJudgment();

        preferences = getSharedPreferences("User", MODE_PRIVATE);

        Global.isLogined = preferences.getBoolean("isLogioned", false);

        if (Global.isLogined)
            handler.sendEmptyMessageDelayed(GO_HOME, TIME);
        else
            handler.sendEmptyMessageDelayed(GO_LOGION, TIME);

    }

    private sPlantAsyncHttpClient sPlantClient;

    private void goHome() {
        Intent i = new Intent(Garden_Welcome_Activity.this, Garden_Main_Activity.class);
        startActivity(i);
        finish();
    }

    private void goLogin() {
        Intent i = new Intent(Garden_Welcome_Activity.this, Garden_Login_Activity.class);
        startActivity(i);
        finish();
    }

    private void login() {
        sPlantClient = sPlantAsyncHttpClient.getInstance();

        String userName = preferences.getString("userName", "");
        String password = preferences.getString("password", "");

        if (userName.length() > 0 && password.length() > 0) {

            RequestParams params = new RequestParams();
            params.put("userName", userName);
            params.put("password", password);

            sPlantClient.post(sPlantAsyncHttpClient.USER_LOGIN, params, new sPlantJsonHttpResponseHandler() {
                        @Override
                        public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                            try
                            {
                                if (response.getInt("succeed")==1)
                                {
                                    JSONObject userInfoJObj = response.getJSONObject("user");

                                    Gson gson = new Gson();
                                    Global.user = gson.fromJson(userInfoJObj.toString(), User.class);

                                    goHome();
                                }
                                else
                                    goLogin();
                            }
                            catch (Exception e)
                            {
                                goLogin();
                            }
                        }
                        @Override
                        public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
                            goLogin();
                        }
                    }
            );

        } else
            goLogin();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (sPlantClient != null)
            sPlantClient.cancelAllRequests(true);

        return super.onKeyDown(keyCode, event);
    }

}
